package view;

import control.KasirKontrol;
import control.OraConnection;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import model.Nota;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;
import tabelmodel.NotaTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Asus
 */
public class frameDaftarTagihan extends javax.swing.JFrame {

    /**
     * Creates new form frameDaftarBilling
     */
    public frameDaftarTagihan() {
        initComponents();
        tampilDaftarTagihan();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelDaftarTagihan = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelDaftarTagihan = new javax.swing.JTable();
        tombolKeluarDaftarTagihan = new javax.swing.JButton();
        tombolDetailDaftarTagihan = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        labelDaftarTagihan.setText("Daftar Tagihan");

        tabelDaftarTagihan.setAutoCreateRowSorter(true);
        tabelDaftarTagihan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Waktu", "No.Nota", "Pembeli", "Pegawai"
            }
        ));
        tabelDaftarTagihan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelDaftarTagihanMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelDaftarTagihan);

        tombolKeluarDaftarTagihan.setText("KELUAR");
        tombolKeluarDaftarTagihan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tombolKeluarDaftarTagihanActionPerformed(evt);
            }
        });

        tombolDetailDaftarTagihan.setText("Detail");
        tombolDetailDaftarTagihan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tombolDetailDaftarTagihanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 739, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(tombolDetailDaftarTagihan)
                        .addGap(331, 331, 331))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(labelDaftarTagihan)
                .addGap(240, 240, 240)
                .addComponent(tombolKeluarDaftarTagihan)
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDaftarTagihan)
                    .addComponent(tombolKeluarDaftarTagihan))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(tombolDetailDaftarTagihan)
                .addGap(37, 37, 37))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tombolKeluarDaftarTagihanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tombolKeluarDaftarTagihanActionPerformed
        this.dispose();
        HomePageDesktop home = new HomePageDesktop();
        home.setVisible(true);
    }//GEN-LAST:event_tombolKeluarDaftarTagihanActionPerformed

    private void tombolDetailDaftarTagihanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tombolDetailDaftarTagihanActionPerformed
        Connection kon = OraConnection.getKoneksi();
        String reportSource = "";
        if (tabelDaftarTagihan.getRowCount() > 0) {
            reportSource = "./iReport/NotaFix.jasper";
        } else {
            reportSource = "./iReport/NotaFix.jasper";
        }
        Map<String, Object> params = new HashMap<String, Object>();
        int baris = tabelDaftarTagihan.getSelectedRow();
        String noNota = tabelDaftarTagihan.getValueAt(baris, 1).toString();
        String pembeli = tabelDaftarTagihan.getValueAt(baris, 2).toString().substring(0, 2).trim();
        String pegawai = tabelDaftarTagihan.getValueAt(baris, 3).toString().substring(0, 6).trim();
        try {
                KasirKontrol.getKoneksiKasir().updateHargaTotal(noNota, pembeli, pegawai);
            } catch (SQLException ex) {
                Logger.getLogger(frameAdmin.class.getName()).log(Level.SEVERE, null, ex);
            }
        params.put("idNota", noNota);
        params.put("noMeja", pembeli);
        params.put("idPegawai", pegawai);

        try {
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportSource, params, kon);
            JasperViewer.viewReport(jasperPrint, false);
        } catch (JRException ex) {
            ex.printStackTrace();
        }
        this.setExtendedState(JFrame.ICONIFIED);
    }//GEN-LAST:event_tombolDetailDaftarTagihanActionPerformed

    private void tabelDaftarTagihanMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelDaftarTagihanMouseClicked
        int baris = tabelDaftarTagihan.getSelectedRow();
        String waktu = tabelDaftarTagihan.getValueAt(baris, 0).toString();
        String noNota = tabelDaftarTagihan.getValueAt(baris, 1).toString();
        String pembeli = tabelDaftarTagihan.getValueAt(baris, 2).toString().substring(0, 2).trim();
        String pegawai = tabelDaftarTagihan.getValueAt(baris, 3).toString().substring(0, 6).trim();
        tombolDetailDaftarTagihan.setEnabled(true);
    }//GEN-LAST:event_tabelDaftarTagihanMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frameDaftarTagihan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frameDaftarTagihan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frameDaftarTagihan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frameDaftarTagihan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frameDaftarTagihan().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelDaftarTagihan;
    private javax.swing.JTable tabelDaftarTagihan;
    private javax.swing.JButton tombolDetailDaftarTagihan;
    private javax.swing.JButton tombolKeluarDaftarTagihan;
    // End of variables declaration//GEN-END:variables

    private void tampilDaftarTagihan() {
        try {
            List<Nota> daftarNota = KasirKontrol.getKoneksiKasir().tampilDaftarTagihan();
            NotaTableModel model = new NotaTableModel(daftarNota);
            tabelDaftarTagihan.setModel(model);

        } catch (SQLException ex) {
            Logger.getLogger(frameAdmin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
